<template>
  <div>

    <el-row justify="center">
      <h1>销售出库</h1>
    </el-row>
    <el-row>
      <el-col :span="22">
        <div  style="display: flex ;justify-content: space-around">
          <el-form-item label="客户" style="width: 250px">
            <el-select v-model="client.id" style="width: 230px">
              <el-option
                  v-for="item in clientList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售员" style="width: 100px">
            {{ this.loginUser.name }}
          </el-form-item>
          <el-form-item label="订单总额" style="width:120px">
            {{updateTotalPrice()}}
          </el-form-item>
          <el-form-item label="送货地址" style="width: 250px">
            <el-input v-model="sendAddress" style="width: 200px;"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="出库物品" style="width:120px">
        </el-form-item>
        <el-table :data="saleCartList" stripe style="margin-bottom: 20px">
          <el-table-column prop="product.name" label="商品名称"></el-table-column>
          <el-table-column prop="product.brand" label="品牌"></el-table-column>
          <el-table-column prop="product.salePrice" label="建议销售价"></el-table-column>
          <el-table-column prop="" label="出库数量">
            <template #default="scope">
              <el-input-number :min="1" v-model="scope.row.num" @change="updateSaleCarNum(scope.row)"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="移除商品">
            <template #default="scope">
              <el-button type="danger" @click="del(scope.row.id)" >移除商品</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="findByItem(1)">添加商品</el-button>
        <el-button type="primary" @click="outKu()">出库</el-button>
        <!--点击添加商品弹出-->
        <el-dialog v-model="dialogVisible" title="添加出库商品" center >
          <!-- 查询 -->
          <div  style="display: flex ;justify-content: space-evenly">
            <el-form-item label="商品名称" style="width: 250px">
              <el-input v-model="paramObj.name" style="width: 200px"></el-input>
            </el-form-item>
            <el-form-item label="类别" style="width: 250px">
              <el-select v-model="paramObj.type">
                <el-option label="" value=""></el-option>
                <el-option label="电脑产品" value="电脑产品"></el-option>
                <el-option label="安防产品" value="安防产品"></el-option>
                <el-option label="办公耗材" value="办公耗材"></el-option>
              </el-select>
            </el-form-item>
            <el-button type="primary" @click="findByItem(1)">查询</el-button>
          </div>
          <el-table :data="cutObj.records" stripe style="margin-bottom: 10px">
            <el-table-column prop="name" label="商品名称"></el-table-column>
            <el-table-column prop="brand" label="品牌"></el-table-column>
            <el-table-column prop="salePrice" label="建议销售价"></el-table-column>
            <el-table-column label="添加">
              <template #default="scope">
                <el-button type="danger" @click="addSaleCar(scope.row.id)">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              background
              layout="prev, pager, next, jumper"
              :total="cutObj.total"
              @current-change="findByItem"
          />

        </el-dialog>
      </el-col>

    </el-row>



  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      totalPrice:0,
      inputType: '',
      inputName: '',
      productList: [],
      currentPage: 1,
      paramObj: {},
      cutObj: {},
      curPage: 1,
      pageSize: 3,
      total: 0,
      dialogVisible: false,
      saleCartList: [],
      saleOrderObj: {},//出库方法的参数，销售订单需要封装 销售员id
      client: {},
      clientList: [],
      loginUser: {},    //销售员（登陆用户对象）
      sendAddress: '',
    }
  },
  methods: {
    findAllClient() {
      //得到所有客户，添加下拉框
      axios.get("/project/client/findAll").then(resp => {
        //查询所有客户
        this.clientList = resp.data;
      })
    },
    //点击添加，查询有库存的商品
    findByItem(pageNO) {
      //展示页面
      this.dialogVisible = true;
      this.currentPage = pageNO;
      axios.get("/project/product/findByItem",{
        params:{
          pageNO,
          name: this.paramObj.name,
          startNum: null,
          endNum: null,
          salePrice: "不限",
          type: this.paramObj.type,
        }
      }).then(resp=>{
        this.cutObj = resp.data
      })
    },
    addSaleCar(productId) {
      axios.get("/project/saleCar/add",{
        params:{
          productId:productId
        }
      }).then(resp =>{
        if (resp.data == "ok"){
          //添加成功，刷新销售车
          this.findByUserId();
          //关闭添加商品界面
          this.dialogVisible = false;
        }
      })
    },
    //通过登陆用户（销售员）id，查询销售车集合
    findByUserId() {
      axios.get("/project/saleCar/findById", {
        params: {
          userId: this.loginUser.id
        }
      }).then(resp => {
        this.saleCartList = resp.data;
        //计算订单总价
        this.updateTotalPrice()
      })
    },
    updateSaleCarNum(saleCarObj){
      axios.get("/project/saleCar/updateNumById",{
        params:{
          saleCarId:saleCarObj.id,
          newNum:saleCarObj.num
        }
      }).then(resp=>{
        if (resp.data=="ok"){
          //更改总价

        }
      })
    },
    del(saleCarId){
      axios.get("/project/saleCar/delById",{params:{saleCarId}}).then(resp=>{
        if (resp.data=="ok"){
          this.findByUserId()
        }
      })
    },
    outKu(){
      //封fk_userId
      this.saleOrderObj.userId=this.loginUser.id
      //封fk_clientId
      this.saleOrderObj.clientId=this.client.id;
      //封so_totalPrice
      this.saleOrderObj.totalPrice=this.totalPrice;
      //封so_address    X
      this.saleOrderObj.address=this.sendAddress;
      axios.get("/project/product/outKu",{params:this.saleOrderObj}).then(resp=>{
        if (resp.data=="ok"){
          //出库完刷新销售车
          this.findByUserId()
        }
      })
    },
    updateTotalPrice(){
      let totalPrice=0;
      this.saleCartList.forEach(n=>{
        return totalPrice+=n.product.salePrice * n.num;
      })
      this.totalPrice=totalPrice;
      return totalPrice;
    }
  },
  created() {
    //查询所有客户
    this.findAllClient();
    //登陆用户就是销售员
    this.loginUser = JSON.parse(sessionStorage.getItem("loginUser"))
    //通过登陆销售员id，查询销售车
    this.findByUserId();

  }
}
</script>

<style scoped>

</style>